package com.lori.netshop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lori.netshop.pojo.TbItemCat;
import com.lori.netshop.service.ItemCatService;
import com.lori.netshop.service.imp.ItemCatServiceimp;

@Controller
@RequestMapping("/item/cat")
public class ItemCatController {
	//自动注入
	@Autowired
	private ItemCatService itemCatServiceimp;
	@RequestMapping("/list")
	@ResponseBody
	public String itemcat(@RequestParam(value="id", defaultValue="0") Long parentId){
		List list = new ArrayList();
		//查询数据库
		List<TbItemCat> find = itemCatServiceimp.find(parentId);
		//遍历数据，将数据存入map中
		for(TbItemCat tbItemCat:find){
			Map map = new HashMap();
			map.put("id", tbItemCat.getId());
			map.put("text", tbItemCat.getName());
			map.put("state", tbItemCat.getIsParent()?"closed":"open");
			list.add(map);
		}
		ObjectMapper mapper = new ObjectMapper();
		String json ="";
		try {
			json = mapper.writeValueAsString(list);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}
}
